import wx

from event_bus import EventBus
from uis import MainView
from view_models import project_state
from views.bare_message import BareMessage
from views.category_manage import CategoryManage
from wxutils import call_after
from .info import ProjectSetup
from .sample_edit import SampleManage
from .sample_view import SamplePreview
from .source_select import SampleSourceMark
from .stat import StatisticsView
from .export_sample import ExportSample


class MainFrame(MainView):
    simple_message: BareMessage

    def __init__(self, parent=None):
        super().__init__(parent)

        self.Bind(wx.EVT_MENU, self.quit_app, self.m_quit)
        self.Bind(wx.EVT_MENU, self.new_project, self.m_new_project)
        self.Bind(wx.EVT_MENU, self.open_project, self.m_open_project)
        self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.on_page_change, self.operate_area)

        EventBus().register_topic(topic="toggle_upgrade_sources", handler=self.toggle_upgrade_prompt)
        EventBus().register_topic(topic="toggle_upgrade_samples", handler=self.toggle_upgrade_prompt)
        EventBus().register_topic(topic="toggle_upgrade_fingerprint", handler=self.toggle_upgrade_prompt)
        EventBus().register_topic(topic="project_changed", handler=self.when_project_changed)

        self.update_notebook_pages()
        self.new_project(None)

    @call_after
    def when_project_changed(self, event):
        if event is not None and "switch" in event:
            self.operate_area.SetSelection(event["switch"])

    def quit_app(self, _):
        self.Close()

    @call_after
    def toggle_upgrade_prompt(self, event):
        if event:
            if not hasattr(self, "simple_message"):
                self.simple_message = BareMessage(self, "正在升级项目数据记录，请稍候……", "数据升级")
            if self.simple_message.IsShown():
                return
            self.simple_message.ShowWindowModal()
        else:
            if hasattr(self, "simple_message") and self.simple_message.IsShown():
                self.simple_message.Hide()

    @staticmethod
    def new_project(_):
        project_state.create()
        EventBus().post_topic(event=None, topic="project_state_updated")

    def open_project(self, _):
        selected_dir = wx.DirDialog(self, "选择项目所在目录")
        if selected_dir.ShowModal() == wx.ID_OK:
            project_state.load(selected_dir.GetPath())
            EventBus().post_topic(event=None, topic="project_state_updated")

    def update_notebook_pages(self):
        self.operate_area.AddPage(ProjectSetup(self.operate_area), "项目信息", select=True)
        self.operate_area.AddPage(CategoryManage(self.operate_area), "特征标记编辑")
        self.operate_area.AddPage(SampleSourceMark(self.operate_area), "样本源图采集")
        self.operate_area.AddPage(SampleManage(self.operate_area), "样本图像编辑")
        self.operate_area.AddPage(SamplePreview(self.operate_area), "样本预览")
        self.operate_area.AddPage(StatisticsView(self.operate_area), "统计")
        self.operate_area.AddPage(ExportSample(self.operate_area), "输出")

    def on_page_change(self, event: wx.BookCtrlEvent):
        page = self.operate_area.GetPage(event.GetSelection())
        if isinstance(page, StatisticsView):
            EventBus().post_topic(event=None, topic="switched_to_statistics")
        elif isinstance(page, SamplePreview):
            EventBus().post_topic(event=None, topic="switched_to_preview")
        elif isinstance(page, CategoryManage):
            EventBus().post_topic(event=None, topic="switched_to_category")
        elif isinstance(page, ExportSample):
            EventBus().post_topic(event=None, topic="switched_to_export")
